<template>
  <div style="width: 80%; margin-right: 10%; margin-left: 10%; margin-top: 2%">
    <el-form label-width="120px">
      <el-form-item label="轮播图标题">
        <el-input v-model="banner.bannerName" placeholder="请填写标题" />
      </el-form-item>

      <el-form-item label="优先级">
        <el-input-number
          :min="1"
          :max="100"
          :step="1"
          v-model="banner.level"
          controls-position="right"
          placeholder="请填写展示优先级"
        />
      </el-form-item>

      <!-- TODO -->
      <!-- 轮播图 -->
      <el-form-item label="轮播图图片">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_URL"
          class="avatar-uploader"
        >
          <img :src="imageUrl" style="width: 320px; height: 320px" />
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button
          v-if="isSave"
          type="primary"
          @click="saveOrUpdateBanner"
          >保存</el-button
        >
        <el-button
          v-else
          type="primary"
          @click="saveOrUpdateBanner"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
// <!-- :action="BASE_API+'/eduoss/fileoss/uploadFile'" -->
<script>
import banner from "@/api/banner";

export default {
  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API + "/weddingoss/fileoss/uploadFile",
      isSave: true,
      banner: {},
      url: "eduoss/fileoss/uploadFile",
      imageUrl:
        "https://img1.baidu.com/it/u=3686273500,1950045651&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672419600&t=40dcde0e36cec07218f8f480555e7dcc",
    };
  },

  watch: {
    $route(to, from) {
      this.init()
    },
  },

  created() {
    this.init()
  },
  methods: {
    init() {
      if (this.$route.query && this.$route.query.id) {
        // this.id = this.$route.query.id
        this.banner.bannerId = this.$route.query.id
        this.isSave = true;
        banner.getOneById(this.banner.bannerId).then((res) => {
          if (res.success) {
            this.banner = res.data.banner;
            this.imageUrl = this.banner.bannerUrl;
          } else {
            this.$message({
              message: "系统异常,稍后再试",
              type: "failed",
            });
          }
        });
      } else {
        this.banner.bannerUrl = this.imageUrl
        this.isSave = false;
      }
    },

    handleAvatarSuccess(res, file) {
      this.banner.bannerUrl = res.data.url
      this.imageUrl = res.data.url
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    saveOrUpdateBanner() {
      banner.saveOrUpdate(this.banner).then(res => {
        if ( res.success ) {
            if ( this.isSave ) {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
            } else {
                this.banner = {}
                this.imageUrl = "https://img1.baidu.com/it/u=3686273500,1950045651&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1672419600&t=40dcde0e36cec07218f8f480555e7dcc"
                this.banner.bannerUrl = this.imageUrl
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
            }
        } else {
            if ( this.isSave ) {
                this.$message({
                  message: "保存失败",
                  type: "error",
                });
            } else {
                this.banner = {}
                this.$message({
                  message: "添加失败",
                  type: "error",
                });
            }
        }
      })
    },
  },
};
</script>

<!-- <style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 148px;
  display: block;
}
</style> -->